home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


You can make a form enforce these standards in its Form_Load event handler. Use the Is operator to see if the current form’s Me variable refers to the same form as the special one form. If you do not want to use the special one form, raise an error if the two values match as in the following code:

Const ERR_ILLEGAL_ONE_FORM = vbObjectError + 1010

Private Sub Form_Load()
    ‘ Make sure this is a new form, not the “one form.”
    If Me Is MyForm Then
    Err.Raise ERR_ILLEGAL_ONE_FORM, _
        “MyForm.Form_Load”, _
        “The one form instance of MyForm was loaded. ” & _
        “You must create new forms using New.”
    End If
End Sub

Note that this test examines only the reference to the one form, not the form itself. That means it does not realize the one form so you do not need to worry about that form being loaded. In fact, using this technique, the one form can never be realized since the form’s Load event handler will raise an error.

If you want to allow only the special one form and prohibit creating forms with the New statement, raise an error if the form’s Me value does not match the one form’s value as in this code:

Const ERR_ILLEGAL_NEW_FORM = vbObjectError + 1011

Private Sub Form_Load()
    ‘ Make sure this is the “one form,” not a new form.
    If Not (Me Is MyForm) Then
    Err.Raise ERR_ILLEGAL_NEW_FORM, _
        “MyForm.Form_Load”, _
        “The program created a new instance of MyForm ” & _
        “using New. It should only use the one form named MyForm.”
    End If
End Sub

The one form that has its own type as its name can cause confusion in several ways. Beware of the one form and the subtle bugs it can cause.

Self-Test

Program Bad3 violates several of the guidelines presented in this chapter. This program, which is shown in Figure 3.2, generates a list of random values and then calculates statistics for them. Click the Randomize button to make the program generate the numbers. Click the Total button to see the numbers’ total. Click the Average button to see their average.

One possible improved version of this code is listed in Appendix A, “Self-Test Solutions.” Note that this is not the only possible solution. You can download updated bad and good versions of this program from the Web at www.vb-helper.com/err.htm.

DefInt I-K

Dim NumTxt

Private Sub Command1_Click()
    ComputeTotal
End Sub

Private Sub Command2_Click()
    Average
End Sub

Private Sub Command3_Click()
    Prepare
End Sub

Public Sub Average()
Dim NumTxt

    For Each ctl In Form1.Controls
        If ctl.Name = ”Text1“ Then NumTxt = NumTxt + 1
    Next ctl
    For i = 0 To NumTxt - 1
        Total = Total + CInt(Text1(i).Text)
    Next i
    Text2.Text = Format$(Total / NumTxt)
End Sub

Public Sub ComputeTotal()
Dim box As Object


Figure 3.2  Program Bad3 displaying the average of a list of random numbers.

    If NumTxt = 0 Then
       For Each box In Form1.Controls
           If box.Name = ”Text1“ Then NumTxt = NumTxt + 1
    Next box
    End If
    For i = 0 To NumTxt - 1
        Total = Total + CInt(Text1(i).Text)
    Next i
    Text2.Text = Format$(Total)
End Sub

Public Sub Prepare()
Dim box As Object

    If NumTxt = 0 Then
       For Each box In Form1.Controls
           If box.Name = ”Text1“ Then NumTxt = NumTxt + 1
    Next box
    End If
    For i = 0 To NumTxt - 1
        Text1(i).Text = Format$(Int(100 * Rnd) + 1)
    Next i
End Sub

Summary

Declaring a variable may seem like a simple thing, but it sets the stage for how the variable is later interpreted. Giving the variable a descriptive name, limiting its scope, and using the most specific data type possible can reduce confusion later. Following these principles and the others summarized by the following Bug Stoppers makes the code easier to understand and can reduce the number of bugs in your project.

BUG STOPPERS: Variables
Explicitly declare variables on separate lines.
Use option explicit.
Don’t use Deftype statements.
Use Hungarian notation.
Use meaningful names.
Capitalize consistently.
Avoid name conflicts.
Limit scope.
Use static variables.
Use specific data types.
Use TypeOf and TypeName.
Avoid variants.
Use variant subtype functions.
Beware the one form.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.